<?php
class BDD
{
	private $ahost;
	private $adbname;
	private $aport;
	private $auser;
	private $apassword;
	private $aconn;
	
	public function __construct()
	{
		$this->ahost='capella.utc.fr';
		$this->ahost='tuxa.sme.utc';
		$this->ahost='localhost'; 		// pour local
		$this->adbname='dbnf17p034';
		$this->adbname='postgres'; 		// pour local
		$this->aport=5432;
		$this->auser='nf17p034';
		$this->auser='postgres'; 		// pour local
		$this->apassword='zywin7UC';
		$this->apassword='postgres'; 	// pour local
		$this->aconn = pg_connect("host=$this->ahost port=$this->aport dbname=$this->adbname user=$this->auser password=$this->apassword");
		$this->query(file_get_contents('bdd.sql'));
		$tables = $this->query("select relname from pg_stat_user_tables");
		while ($table = $this->fetch($tables))
		{
			$tablename = $table->relname;
			$this->query("copy $tablename from '".dirname($_SERVER["SCRIPT_FILENAME"])."/data_$tablename.sql';");
		}
	}
	public function query($pq)
	{
		return pg_query($this->aconn, $pq);
	}
	public function fetch($res)
	{
		return pg_fetch_object($res);
	}
	public function __destruct()
	{
		$tables = $this->query("select relname from pg_stat_user_tables");
		while ($table = $this->fetch($tables))
		{
			$tablename = $table->relname;
			$this->query("copy $tablename to '".dirname($_SERVER["SCRIPT_FILENAME"])."/data_$tablename.sql';");
		}
		pg_close($this->aconn);
	}

}
?>